package com.quotientintelligence.caftrak.shared;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.csvreader.CsvReader;
import com.quotientintelligence.caftrak.shared.Substance.ActiveIng;
import com.quotientintelligence.caftrak.shared.Substance.MUnits;


@SuppressWarnings("unused")
public class CafLoader {
	private String filename ;
	public CafLoader() {this("CafswSug.csv");}
	public CafLoader(String myfile) {filename = myfile;}
	

	public List<Drink> read() throws IOException {
		List<Drink> drinks = new ArrayList<Drink>();
		CsvReader reader = new CsvReader(filename);
		reader.readHeaders();
		while (reader.readRecord())	{
			Drink d = readOne(reader);
			drinks.add(d);
		}
			reader.close();
			return drinks;
	}
	
	private Drink readOne(CsvReader reader)  {
		Drink dr =null;
		 try {
			 Substance sub = new Substance(
						ActiveIng.Caffeine,
						Integer.parseInt(reader.get("Caffeine_In_mg")),
						MUnits.milligram);
			 dr = new Drink(
						reader.get("Drink"),
						Float.parseFloat(reader.get("Ounces")));
			 dr.addIngredient(sub);
			 String name2 = reader.get("SugarName");
			 float oz2 = Float.parseFloat(reader.get("SugarOunces"));
	
			 if (name2.equals(dr.Name) && oz2 == dr.Volume) {
				 sub = new Substance(ActiveIng.Sugar,
						 Integer.parseInt(reader.get("Sugar_in_grams")),
						 MUnits.gram);
				 dr.addIngredient(sub);				 
			 }
		 } catch (Exception e) {}
		 
		 return dr;		
	}
}
